package cn.wyl.pet.controller;

import cn.wyl.pet.service.IPetService;
import cn.wyl.pet.domain.Pet;
import cn.wyl.pet.query.PetQuery;
import cn.wyl.basic.util.AjaxResult;
import cn.wyl.basic.util.PageList;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * 后端接口类；
 */
@RestController
@RequestMapping("/pet")
public class PetController {

    @Autowired
    public IPetService petService;

    /**
     * 接口：添加或修改
     *
     * @param pet 传递的实体
     * @return AjaxResult 响应给前端
     */
    @PutMapping
    public AjaxResult addOrUpdate(@RequestBody Pet pet) {
        try {
            if (pet.getId() != null) {
                petService.update(pet);
            } else {
                petService.save(pet);
            }
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false, "操作失败");
        }
    }

    /**
     * 接口：删除
     *
     * @param id
     * @return AjaxResult 响应给前端
     */
    @DeleteMapping(value = "/{id}")
    public AjaxResult delete(@PathVariable("id") Long id) {
        try {
            petService.delete(id);
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false, "删除失败");
        }
    }

    /**
     * 接口：批量删除
     *
     * @param ids
     * @return AjaxResult 响应给前端
     */
    @PatchMapping
    public AjaxResult patchDelete(@RequestBody Long[] ids) {
        try {
            petService.patchDelete(ids);
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false, "批量删除失败");
        }
    }

    /**
     * 接口：查询单个对象
     *
     * @param id
     */
    @GetMapping("/{id}")
    public Pet findOne(@PathVariable("id") Long id) {
        return petService.findById(id);
    }


    /**
     * 接口：查询所有
     *
     * @return
     */
    @GetMapping
    public List<Pet> findAll() {
        return petService.findAll();
    }


    /**
     * 接口：分页查询或高级查询
     *
     * @param query 查询对象
     * @return PageList 分页对象
     */
    @PostMapping
    public PageList<Pet> queryPage(@RequestBody PetQuery query) {
        return petService.queryPage(query);
    }

    /**
     * 上架宠物
     * @param ids
     * @return
     */
    @PatchMapping("/onsale")
    public AjaxResult onsale(@RequestBody Long[] ids, HttpServletRequest request) {
        try {
            return petService.onsale(ids,request);
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false, "上架宠物失败");
        }
    }

    /**
     * 下架宠物
     * @param ids
     * @return
     */
    @PatchMapping("/offsale")
    public AjaxResult offsale(@RequestBody Long[] ids) {
        try {
            petService.offsale(ids);
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false, "下架宠物失败");
        }
    }

}
